//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Random;
import javax.imageio.ImageIO;
import model.User;
import service.UserService;
import service.impl.UserServicelmpl;

@WebServlet({"/register"})
public class RegisterServlet extends HttpServlet {
    UserService userService = new UserServicelmpl();

    public RegisterServlet() {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.generateCode(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String vcode = request.getParameter("vcode");
        String code = (String)request.getSession().getAttribute("code");
        if (!code.equals(vcode)) {
            request.setAttribute("msg", "验证码错误");
            request.getRequestDispatcher("/register.jsp").forward(request, response);
        }

        User userInDB = null;

        try {
            userInDB = this.userService.getUserByName(username);
        } catch (SQLException var10) {
            throw new RuntimeException(var10);
        }

        if (userInDB != null) {
            request.setAttribute("msg", "当前用户名已存在");
            request.getRequestDispatcher("/register.jsp").forward(request, response);
        } else {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            int flag = this.userService.addUser(user);
            if (flag > 0) {
                request.setAttribute("msg", "注册成功，请登录");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            } else {
                request.setAttribute("msg", "注册失败，请稍后重试！");
                request.getRequestDispatcher("/register.jsp").forward(request, response);
            }
        }

    }

    private void generateCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Random random = new Random(System.currentTimeMillis());
        StringBuilder sb = new StringBuilder();
        char sc = 'a';

        for(int i = 0; i <= 4; ++i) {
            int str = random.nextInt(62);
            if (str < 10) {
                sb.append(str);
            } else {
                char a;
                if (str < 36) {
                    sc = (char)(str - 10 + 97);
                    sb.append(sc);
                } else {
                    sc = (char)(str - 36 + 65);
                    sb.append(sc);
                }
            }
        }

        BufferedImage image = new BufferedImage(60, 20, 1);
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.GRAY);
        graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("Time", 1, 18));
        graphics.drawString(sb.toString(), 3, 17);
        graphics.dispose();
        ImageIO.write(image, "JPG", response.getOutputStream());
        System.out.println("生成的验证码：" + sb);
        request.getSession().setAttribute("code", sb.toString());
    }
}